package org.kolonitsky.alexey.preloader
{
    import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.IEventDispatcher;
    import flash.utils.*;
    
    import ui.control.AjaxLoader;


    /**
     *  This is pretty simple preloader. It quickly loaded and display standart
     *  ajax loader (rounded circle).
     *
     *  After application loaded is show it.
     *
     *  For use this preloader main class must have metatag
     *
     *  <code>
     *  [Frame(factoryClass="org.kolonitsky.alexey.utils.Preloader")]
     *  </code>
     *
     *  and class ApplicationCreater with must be defined and implement
     *  interface IApplicationCreater.
     */
     public class Preloader extends MovieClip
     {
        //-----------------------------
        // Constructor
        //-----------------------------

        public function Preloader()
        {
            super();
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            informer = preloaderSkinClass;
            addChild(informer);
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }
		
		//---------------------------------------------------------------------
		//
		//  Protected 
		//
		//---------------------------------------------------------------------
		
		/**
		 *  Readonly property with application class name. Application instance 
		 *  created using this name.
		 */
		protected function get applicationClassName():String
		{
			return "Main"
		}

		protected function get preloaderSkinClass():DisplayObject
		{
			return new AjaxLoaderClass();
		}


        //---------------------------------------------------------------------
        //
        //  Private logic
        //
        //---------------------------------------------------------------------

		[Embed(source="/assets/ajaxLoader.swf", symbol="loader")]
		private var AjaxLoaderClass:Class;       

        private var informer:DisplayObject;

        private var application:DisplayObject;

        private function enterFrameHandler(event:Event):void
        {
            // loading complete
            if(framesLoaded == totalFrames)
            {
                removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
                removeChild(informer);
                nextFrame();
                
                init();                
            }
            else
            {
                var percent:Number = 100 * root.loaderInfo.bytesLoaded / root.loaderInfo.bytesTotal;
                informer["tfPercent"].text = percent.toFixed(0) + "%";
                if (stage)
                {
                    informer.x = stage.stageWidth / 2;
                    informer.y = stage.stageHeight / 2;
                }
            }
        }

        private function init():void
        {
            var mainClass:Class = null;

            try
            {
                mainClass = Class(getDefinitionByName(applicationClassName));
                if(mainClass)
                {
                    application = new mainClass();
                    addChild(application);
                }
            }
            catch (error:Error)
            {
                switch(error.errorID)
                {
                    case 1065:
                    {
                        trace("ERROR: Not defined ApplicationCreator class. " +
                            "Please add an IApplicationCreator implementation" +
                            " in project.");
                        break;
                    }

                    default:
                    {
                        trace("ERROR: " + error.message);
                        break;
                    }
                }
            }
        }
    }
}